import { _decorator, Component, Node, AudioSource, assert,AudioClip } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AudioController')
export class AudioController extends Component {
    @property(AudioClip)
    public bgmClip: AudioClip = null!;  

    @property(AudioClip)
    public gameOverClip: AudioClip = null!;  

    @property(AudioClip)
    public growPlantClip: AudioClip = null!;  

    @property(AudioClip)
    public jumpClip: AudioClip = null!;

    @property(AudioClip)
    public startBgmClip: AudioClip = null!;  

    @property(AudioClip)
    public upgradeClip: AudioClip = null!;  

    @property(AudioClip)
    public winClip: AudioClip = null!;  

    @property(AudioClip)
    public plantAttackedClip: AudioClip = null!;  

    @property(AudioClip)
    public repeaterAttackClip: AudioClip = null!;  

    @property(AudioClip)
    public enemyDie: AudioClip = null!;  

    @property(AudioClip)
    public enemyAttacked: AudioClip = null!;  

    @property(AudioSource)
    public _audioSource: AudioSource = null!;

    onLoad () {
        // 获取 AudioSource 组件
        const audioSource = this.node.getComponent(AudioSource)!;
        // 检查是否含有 AudioSource，如果没有，则输出错误消息
        assert(audioSource);
        // 将组件赋到全局变量 _audioSource 中
        this._audioSource = audioSource;
    }

    playBgm () {
        // 播放音乐
        this._audioSource.play();
    }
    playStartBgm(){
        this._audioSource.clip = this.startBgmClip;
        this._audioSource.play();
    }
    playMainBgm(){
        this._audioSource.clip = this.bgmClip;
        this._audioSource.play();
    }
    pause () {
        // 暂停音乐
        this._audioSource.pause();
    }
    stop(){
        this._audioSource.stop();
    }
    playOneShot (type) {
        this._audioSource.playOneShot(this[type], 1);
    }
}

